package com.uusee.crawler.pageprocessor.baikevod;

import java.util.Date;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.uusee.crawler.framework.Processor;
import com.uusee.crawler.model.CrawlStatusCodes;
import com.uusee.crawler.model.CrawlURI;
import com.uusee.crawler.util.PageProcessorUtils;
import com.uusee.shipshape.bk.model.BaikeVodDataSource;

public class BaikeVodDataSourcePageProcessor extends Processor{
	private static Log log = LogFactory.getLog(BaikeVodDataSourcePageProcessor.class);
	private final Properties processPageRegex;
	
	public BaikeVodDataSourcePageProcessor(Properties processPageRegex) {
		this.processPageRegex = processPageRegex;
	}

	protected void innerProcess(CrawlURI crawlUri) {
		if (!canProcess(crawlUri)) {
			return;
		}
		try {
			String sourceSite = crawlUri.getSourceSite();
			String crawlUrl = crawlUri.getCrawlUrl();
			String crawlResult = crawlUri.getCrawlResult();
			
			String domainReg = "http://(.*?)/";
			String domain = PageProcessorUtils.getValue(domainReg, crawlUrl);

			String modelText = crawlResult.replaceAll("&nbsp[;]?", "");
			
			BaikeVodDataSource baikeVod = new BaikeVodDataSource();
			Set<Object> keySet = processPageRegex.keySet();
			for (Object key:keySet) {
				String name = (String)key;
				if (processPageRegex.containsKey(name)) {
					String value = PageProcessorUtils.getValue(processPageRegex.getProperty(name), modelText);
					if (StringUtils.isNotEmpty(value)&&PropertyUtils.isWriteable(baikeVod, name)) {
						value = StringUtils.trim(value);
						PropertyUtils.setProperty(baikeVod, name, value);
						
					}
					//System.out.println(name+"="+value);
				}
			}
			
			baikeVod.setUpdateUser("crawler");
			baikeVod.setUpdateDate(new Date());
			crawlUri.setModel(baikeVod);
			crawlUri.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			log.equals(e);
			crawlUri.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_EXCEPTION);
		}
	}
	
	private boolean canProcess(CrawlURI crawlURI) {
		int crawlStatus = crawlURI.getCrawlStatus();
		if (crawlStatus == CrawlStatusCodes.FETCH_SUCCESS) {
			return true;
		} else if (crawlStatus == CrawlStatusCodes.FETCH_SC_NOT_OK) {
			crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_INVALID);
			return false;
		} else {
			return false;
		}
	}
}
